package com.nbjtjc.safe.api.emg.goods;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgGoodsMapper;
import com.nbjtjc.safe.model.emg.EmgGoods;
import com.nbjtjc.safe.service.emg.EmgGoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 物资
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgGoods")
public class GoodsController extends AbstractController<EmgGoods> {
    @Autowired
    private EmgGoodsMapper emgGoodsMapper;

    @Autowired
    private EmgGoodsService emgGoodsService;

    @Override
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SysLog("删除物资")
    public Result delete(int id) {
        Result result = new Result();
        try {
            emgGoodsMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @SysLog("添加或者更新物资")
    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST})
    public Result add(EmgGoods emgGoods) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgGoods)) {
            return result;
        }
        try {
            result = emgGoodsService.add(emgGoods);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/find", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "/find", notes = "查找资源，不传deptId 表示搜索本部门的物资，需要搜索其他部门的物资，传deptId即可")
    public Result find(EmgGoods emgGoods, Page page) {
        Result result = new Result();
        try {
//            if (ObjectUtil.isNull(emgGoods)) {
//                emgGoods = new EmgGoods();
//                SysUser sysUser = (SysUser) MyShiroUtils.getSubject().getSession().getAttribute("user");
//                emgGoods.setDeptId(sysUser.getDeptId().intValue());
//            }
//            if (ObjectUtil.isNull(emgGoods.getDeptId())) {
//                SysUser sysUser = (SysUser) MyShiroUtils.getSubject().getSession().getAttribute("user");
//                emgGoods.setDeptId(sysUser.getDeptId().intValue());
//            }
            PageHelper.startPage(page.getPageCurrent(), page.getPageSize());
            List<EmgGoods> emgGoodsList = emgGoodsMapper.search(emgGoods, page);
            result.setData(new PageInfo<>(emgGoodsList));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @GetMapping("/countByCategory")
    @ApiOperation(value = "/countByCategory",notes = "按种类对物资进行数量统计")
    public Result countByCategory(){
        Result result=new Result();
        result.setData(emgGoodsService.countByCategory());
        return result;
    }

    @RequestMapping(value = "/findPlanPublicGoods", method = {RequestMethod.POST})
    @ApiOperation(value = "/findPlanPublicGoods", notes = "查找资源，不传deptId 表示搜索本部门的物资，需要搜索其他部门的物资，传deptId即可")
    public Result findPlanPublicGoods(@RequestParam("emgGoodsNames")List<String> emgGoodsNames, Page page) {
        Result result = new Result();
        try {
            if(CollectionUtil.isEmpty(emgGoodsNames)){
                result.setFalse();
                return result;
            }
            List<EmgGoods> emgGoodsList =new ArrayList<>();
            EmgGoods emgGoods=new EmgGoods();
            page.setPageSize(100);
            emgGoodsNames.forEach(emgGoodsName->{
                emgGoods.setGoodsName(emgGoodsName);
                emgGoodsList.addAll(emgGoodsMapper.search(emgGoods,page));
            });
            result.setData(emgGoodsList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
